﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using FinalTestMachine.Motion.Product;

namespace FinalTestMachine.Connect
{
    public class UdpItemData
    {

        public string Name { get; set; }
        public string Value { get; set; }
        public string Result { get; set; }

        public Brush bdBrush { get; set; } = Brushes.White;

        public void Copy(UdpItemData upDataMsg)
        {
            Type type = upDataMsg.GetType();
            PropertyInfo[] fieldInfos_Copy = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            PropertyInfo[] propertyInfos_this = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var item in fieldInfos_Copy)
            {
                foreach (var item2 in propertyInfos_this)
                {
                    if (item.Name == item2.Name)
                    {
                        if (item2.CanWrite)
                        {
                            item2.SetValue(this, item.GetValue(upDataMsg));
                        }
                        break;
                    }
                }
            }
        }

        public static List<UdpItemData> GetUdpItemsFromProductTestStationTestItem(List<ProductTestStationTestItem> productTestStationTestItems)
        {
            if (productTestStationTestItems == null) return null;
            List<UdpItemData> udpItemDatas = new List<UdpItemData>();
            foreach (var item in productTestStationTestItems)
            {
                UdpItemData udpItemData = new UdpItemData();
                udpItemData.Result = item.result;
                udpItemData.Name = $"{item.name}[{item.lowLimit},{item.upLimit}]";
                udpItemData.Value = item.value;
                switch (udpItemData.Result.ToUpper())
                {
                    case "1":
                    case "OK":
                    case "PASS":
                        udpItemData.bdBrush = Brushes.LightGreen;
                        break;

                    case "0":
                    case "NG":
                    case "FAIL":
                        udpItemData.bdBrush = Brushes.Red;
                        break;

                    default:
                        udpItemData.bdBrush = Brushes.Gray;
                        break;
                }
                udpItemDatas.Add(udpItemData);
            }

            return udpItemDatas;
        }
    }
}
